package api;

import Dao.Blog;
import Dao.BlogDao;
import Dao.User;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-10-28
 * Time: 16:56
 */
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    //Jackson ObjectMapper类(com.fasterxml.jackson.databind.ObjectMapper)解析JSON工具
    //
    //序列化：      Java对象生成Json
    //
    //反序列化：  Json解析为Java对象
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BlogDao blogDao=new BlogDao();
        //尝试获取blogId这个参数，根据是否能获取到这个参数，将其进行分支
        String blogId=req.getParameter("blogId");
        if(blogId==null){
            //说明此处是需要获取博客列表
            List<Blog> blogs =blogDao.getblogs();
            String respJson=objectMapper.writeValueAsString(blogs);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respJson);
        }else{
            //说明此处要获得某篇博客的详情页 获取一篇博客
            System.out.println(Integer.parseInt(blogId));
            Blog blog = blogDao.getBlog(Integer.parseInt(blogId));
            System.out.println(blog.toString());
            //如果该篇博客不存在，那么此时的blog==null
            if(blog==null){
                blog=new Blog();
            }
            String respJson=objectMapper.writeValueAsString(blog);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respJson);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取登录的用户
        //虽然编写内容的时候，一定会有用户登录，但是为了防止其它情况，我们还是要做检查
        HttpSession session=req.getSession(false);
        if(session==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("user don`t register,please register");
            return;
        }
        User user= (User) session.getAttribute("user");
        if(user==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("user don`t register,please register");
            return;
        }
        //2.从请求中获取用户传递过来的信息
        req.setCharacterEncoding("utf8");
        String title=req.getParameter("title");
        String content=req.getParameter("content");
        if(title==null || content==null || "".equals(title) || "".equals(content)){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("title or content is empty");
            return;
        }
        //3.构造对象，将其插入倒数据库中
        Blog blog=new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());
        //此处的时间是sql通过自带的now获取，不需要手动获取
        BlogDao blogDao=new BlogDao();
        blogDao.insert(blog);
        //4.跳转到列表页
        resp.sendRedirect("blog_list.html");
    }
}
